<?php

    require_once 'lib/common.php';
    require_once 'lib/file.php';
    
    if (!extension_loaded('zip'))
        exit(quitWithError('Zip Extension', 'Zip Extension is not loaded!'));
    
    $zipFile = isset($_GET['f']);
    
    if ($zipFile)
        $zipFile = base64_decode($_GET['f']);
           
    if (!file_exists($zipFile))
        $zipFile = false;
    
    if ($zipFile)
    {  
        $f      = new File($zipFile);
        $path   = new Dir($f->getPath());
        
        initSite('Extract archive', $f->getFilename());      
        
        $opt[] = array(
            'href' => '.?v=' . $f->getPath(),
            'text'  => 'Go Back to directory'
        );
            
        $zip = new ZipArchive();
        $zip->open($zipFile);
        
    
        if (isset($_GET['do'])):
        	// If GET[here] is set then files are extracted 
        	// in the containing folder
        	// else
        	// they are extracted in a subfolder of their own
        	// with the filename also being the folder name
        	$targetDir = isset($_GET['here']) ? $f->getPath() : includeTrailingBackslash($f->getPath()) . $f->getFilename(false); 
 
            if (@$zip->extractTo($targetDir))
                showInformation('Extracting Archive ...', 'Extracting Performed successfully', $opt);
            else
                showFatalError('Extracting Archive ...', "Unable to extract selected file {$zipFile} to {$f->getPath()}");
		else: ?>
            <center>
            <div id="wrapper">
                <h2><img src="./img/file_extension_zip.png" alt=""> Extracting <?php echo $f->getFilename() ?></h2>
                <div class="separator"></div>        
                <h3><img src="./img/information.png"> 
                        Filesize: <?php echo intToFileSize($f->getSize()) ?> /
                    <img src="./img/delete16.png">  
                        Warning: This operation will overwrite any existing files</h3>
            
                <a href='.?v=<?php echo $f->getPath() ?>' class="hrefbtn">Return to "<?php echo $path->getName() ?>"<a>   
				<a href='?f=<?php echo $_GET['f'] ?>&here&do' class="hrefbtn">Extract files<a>
				<a href='?f=<?php echo $_GET['f'] ?>&do' class="hrefbtn">Extract to "<?php echo $f->getFileName(false) ?>"</a>
            </div>  
            </center>      
		<?php endif;
        
        finishSite();
    }
    else
        quitWithError('Extracting Archive ...', "Selected file does not exist");
		
		
		
		
		
		
		
		
		
		
		
		
		
		